﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;


namespace Zgq.CommonTools
{
    public static class EncryptHelper
    {
        private static byte[] Key = {
                                        0x1, 0x13, 0x1, 0x1, 0xB1, 0xE1, 0x1, 0xB1,
                                        0x1, 0x1, 0x1, 0x16, 0x1, 0x1, 0x1, 0xE1,
                                        0xC1, 0x1, 0xA1, 0x91, 0x1, 0xB1, 0x1, 0x1,
                                        0x1, 0x1, 0x1, 0x61, 0x1, 0xC1, 0x1, 0x1 };

        private static byte[] IV = {
                                        0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1,
                                        0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1 };

        public static string EncryptorString(string str)
        {
            RijndaelManaged rij = new RijndaelManaged();

            Rijndael crypt = Rijndael.Create();
            crypt.Key = EncryptHelper.Key;
            crypt.IV = EncryptHelper.IV;

            using (ICryptoTransform transform = crypt.CreateEncryptor())
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, transform, CryptoStreamMode.Write))
                    {
                        byte[] byteInCode = System.Text.UTF8Encoding.UTF8.GetBytes(str);

                        cs.Write(byteInCode, 0, byteInCode.Length);
                        cs.FlushFinalBlock();

                        byte[] byteOut = ms.ToArray();
                        return Convert.ToBase64String(byteOut);
                    }
                }
            }
        }

        public static string DecryptorString(string str)
        {
            RijndaelManaged rij = new RijndaelManaged();

            Rijndael crypt = Rijndael.Create();
            crypt.Key = EncryptHelper.Key;
            crypt.IV = EncryptHelper.IV;

            using (ICryptoTransform transform = crypt.CreateDecryptor())
            {
                byte[] byteInCode = Convert.FromBase64String(str);

                using (MemoryStream ms = new MemoryStream(byteInCode, 0, byteInCode.Length))
                {
                    using (CryptoStream cs = new CryptoStream(ms, transform, CryptoStreamMode.Read))
                    {
                        using (StreamReader sr = new StreamReader(cs))
                        {
                            return sr.ReadToEnd();
                        }
                    }
                }
            }
        }

        #region MD5加密
        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="Password">需加密字符串</param>
        /// <returns>加密过后字符串</returns>
        public static string GetPassword(string Password)
        {
            if (Password == null) { Password = ""; }
            if (!string.IsNullOrEmpty(Password))
            {
                byte[] result = Encoding.Default.GetBytes(Password.Trim());
                MD5 md5 = new MD5CryptoServiceProvider();
                return BitConverter.ToString(md5.ComputeHash(result)).Replace("-", "");
            }
            else
            {
                return Password;
            }
        }
        #endregion
    }
}
